package com.zhang.test2;

import org.junit.Test;

import java.io.IOException;
import java.util.logging.*;


//硬编码
public class HardCode {


    //写法
    @Test
    public void test9() {

        //2.创建日志对象
        Logger logger = Logger.getLogger("a");

        //3.打印日志
        //写法一
        logger.log(Level.INFO, "info level1");
        //写法二
        logger.info("info level2");

    }


    //占位符
    @Test
    public void test1() {
        //2.创建日志对象
        Logger logger = Logger.getLogger("com.zhang.test2.Test4");


        //3.打印日志
        String name="zf";
        int age = 23;
        logger.log(Level.INFO,"student name:{0},age:{1}",new Object[]{name,age});

    }

    //日志级别
    @Test
    public void test2() {

            Logger logger = Logger.getLogger("a");

            logger.finest( "finest level");
            logger.finer("finer level");
            logger.fine("fine level");
            logger.config( "config level");


            logger.info("info level");
            logger.warning("warning level");
            logger.severe("severe level");

    }

    //Logger之间的父子关系
    @Test
    public void test3() {
        Logger logger1 = Logger.getLogger("com.zhang.test2");
        Logger logger2 = Logger.getLogger("com.zhang.test2.Test4");

        System.out.println("logger1全类名:"+logger1.getParent());
        System.out.println("logger2全类名:"+logger2.getParent());

        System.out.println("logger1父类名字:"+logger1.getParent().getName());//打印不出来是RootLogger RootLogger是所有Logger的顶级Logger
        System.out.println("logger2父类名字:"+logger2.getParent().getName());//com.zhang.test2

    }



    //子类默认继承了父类的设置
    @Test
    public void test4() {
        Logger logger1 = Logger.getLogger("com.zhang.test2");
        Logger logger2 = Logger.getLogger("com.zhang.test2.Test4");


        //设置父亲
        logger1.setUseParentHandlers(false);//不使用logger1的父类设置
        ConsoleHandler handler = new ConsoleHandler();
        SimpleFormatter formatter = new SimpleFormatter();
        handler.setFormatter(formatter);
        logger1.addHandler(handler);
        handler.setLevel(Level.ALL);
        logger1.setLevel(Level.ALL);


        //儿子打印(子类继承了父类,因此子类Level=父类level +使用父类处理器)
        logger2.finest( "finest level");
        logger2.finer("finer level");
        logger2.fine("fine level");
        logger2.config( "config level");
        logger2.info("info level");
        logger2.warning("warning level");
        logger2.severe("severe level");


    }

    //子类默认继承父类后,也还可对子类本身设置
    @Test
    public void test5() {
        Logger logger1 = Logger.getLogger("com.zhang.test2");
        Logger logger2 = Logger.getLogger("com.zhang.test2.Test4");


        //设置父亲
        logger1.setUseParentHandlers(false);//不使用logger1的父类设置
        ConsoleHandler handler = new ConsoleHandler();
        SimpleFormatter formatter = new SimpleFormatter();
        handler.setFormatter(formatter);
        logger1.addHandler(handler);
        handler.setLevel(Level.ALL);
        logger1.setLevel(Level.ALL);


        //儿子打印(子类继承了父类,因此子类Level=父类level +使用父类处理器)
        logger2.setLevel(Level.INFO);
        logger2.finest( "finest level");
        logger2.finer("finer level");
        logger2.fine("fine level");
        logger2.config( "config level");
        logger2.info("info level");
        logger2.warning("warning level");
        logger2.severe("severe level");


    }

    //关闭父类设置,只采用子类本身的设置
    @Test
    public void test6() {
        Logger logger1 = Logger.getLogger("com.zhang.test2");
        Logger logger2 = Logger.getLogger("com.zhang.test2.Test4");


        //设置父亲
        logger1.setUseParentHandlers(false);//不使用logger1的父类设置
        ConsoleHandler handler = new ConsoleHandler();
        SimpleFormatter formatter = new SimpleFormatter();
        handler.setFormatter(formatter);
        logger1.addHandler(handler);
        handler.setLevel(Level.ALL);
        logger1.setLevel(Level.ALL);


        //设置子类
        logger2.setUseParentHandlers(false);//关闭父类的设置(不使用logger1这个父类的设置)
        ConsoleHandler handler2 = new ConsoleHandler();
        SimpleFormatter formatter2 = new SimpleFormatter();
        handler2.setFormatter(formatter2);
        logger2.addHandler(handler2);
        handler2.setLevel(Level.INFO);
        logger2.setLevel(Level.WARNING);



        logger2.finest( "finest level");
        logger2.finer("finer level");
        logger2.fine("fine level");
        logger2.config( "config level");
        logger2.info("info level");
        logger2.warning("warning level");
        logger2.severe("severe level");


    }

    //日志的Level和handler的level关系
    @Test
    public void test7() {
        Logger logger1 = Logger.getLogger("com.zhang.test2");


        //设置父亲
        logger1.setUseParentHandlers(false);//不使用logger1的父类设置
        ConsoleHandler handler = new ConsoleHandler();
        SimpleFormatter formatter = new SimpleFormatter();
        handler.setFormatter(formatter);
        logger1.addHandler(handler);

        logger1.setLevel(Level.ALL);
        handler.setLevel(Level.INFO);


        logger1.finest( "finest level");
        logger1.finer("finer level");
        logger1.fine("fine level");
        logger1.config( "config level");
        logger1.info("info level");
        logger1.warning("warning level");
        logger1.severe("severe level");

    }

    //硬编码解释
    @Test
    public void test8() {

        //配置logger1
        //1.创建logger对象
        Logger logger1 = Logger.getLogger("com.zhang.test2");
        //2.不使用logger1的父类设置
        logger1.setUseParentHandlers(false);

        //3.创建handler日志处理器
        ConsoleHandler handler = new ConsoleHandler();
        //4.创建格式对象
        SimpleFormatter formatter = new SimpleFormatter();
        //5.设置日志处理器输出格式
        handler.setFormatter(formatter);
        //6.设置日志处理器的Level
        handler.setLevel(Level.INFO);


        //7.Logger添加日志处理器
        logger1.addHandler(handler);
        //8.设置Logger的Level
        logger1.setLevel(Level.ALL);




        logger1.finest( "finest level");
        logger1.finer("finer level");
        logger1.fine("fine level");
        logger1.config( "config level");
        logger1.info("info level");
        logger1.warning("warning level");
        logger1.severe("severe level");

    }


}
